<?php

namespace App\Console\Commands;

use App\Models\Order;
use App\Models\OrderChild;
use App\Models\OrderSecond;
use App\Rep\OrderRep;
use Illuminate\Console\Command;

class AotoAddFinishProduct extends Command
{
    /**
     * The name and signature of the console command.
     * php artisan command:aotoaddfinishproduct
     * @var string
     */
    protected $signature = 'command:aotoaddfinishproduct';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $order_list = Order::where('is_del',2)
            ->where('pid',1)
            ->whereIn('production_status',[0,1])->get();
        //p($order_list->toArray());
        foreach($order_list as $k=>$v){
            //$this->info('order_id='.$v->id);
            $res_count = OrderChild::where('order_id',$v->id)->whereNull('submit_time')->count();
            //p($res_count);
            if($res_count == 0){
                $this->info('order_id='.$v->id);
                $order_second_list = OrderSecond::where('order_id',$v->id)->get();
                if($order_second_list->count()>0){
                    foreach($order_second_list as $kk=>$vv){
                        //$this->info('order_second_id='.$vv->id);
                        $vv->production_status = 2;
                        $vv->save();
                    }
                }
                $v->production_status = 3;
                $v->save();
                (new OrderRep())->selectOrderChildStatusByOrderId($v->id);
            }

        }
    }
}
